$(function() {
	//console.log(getCookieValue("token"),888)
	$('#login-btn').on('tap', function() {
		if(!$('#peoplephone').val()) {
			mui.toast('请输入推荐人手机号');
			return false;
		};

		var peopletel = $('#peoplephone').val();
		var reg = /(1[3-9]\d{9}$)/;
		if(!reg.test(peopletel)) {
			mui.toast('请输入正确的手机格式');
			return false;
		}

		if(!$('#phone').val()) {
			mui.toast('请输入手机号');
			return false;
		};
		var tel = $('#phone').val();
		if(!reg.test(tel)) {
			mui.toast('请输入正确的手机格式');
			return false;
		}
		var mypwd = /^(?![0-9]+$)(?![a-zA-Z]+$)(?!([^(0-9a-zA-Z)]|[])+$)([^(0-9a-zA-Z)]|[]|[a-zA-Z]|[0-9]){6,}$/;
		var pas = $('#password').val();
		if(!mypwd.test($('#password').val())) {
			mui.toast('请输入数字，符号，字母任意两种混合的密码');
			return false;
		};
		var pass = $('#name').val()
		if(!$('#name').val()) {
			mui.toast('请再次输入密码');
			return false;
		};
		if($('#name').val() != $('#password').val()) {
			mui.toast('密码输入不一致');
			return false;
		}

		var yanzheng = $('#phonecode').val()
		if(!$('#phonecode').val()) {
			mui.toast('请输入验证码');
			return false;
		}
		var rule = $('#rulelogin').is(':checked');
		if(!rule == true) {
			mui.toast('请先同意用户协议');
			return false;
		};

		newuserLogin({
			userTel: tel,
			password: pas,
			confirmPassword: pass,
			parentAccountPhone: peopletel,
			verificationCode: yanzheng
		}, function(data) {
			//console.log(data)
			setCookie('token', data.data.token, 8000, '/');
			getCookieValue("token",999)
			//console.log(getCookieValue("token",999))
			localStorage.setItem('userUuid', data.data.userUuid);
			
			if(data.code == 200) {
				//获取codeUrl
				getCodeUrl(function(data) {
					var msgAddress = data.msg;
					console.log(msgAddress);
					//获取支付数据
					var newAddress = msgAddress.replace("redirect_uri=http%3A%2F%2Fwww.leyouhui888.cn/weChatPay/getAccessToken", "redirect_uri=http://www.leyouhui888.cn/card/l-newuserlogin-recharge.html")
					window.location = newAddress;
					console.log(newAddress)
				})
				
			}else {
					mui.toast(data.msg)
				}
			
		});

	});

	//获取验证码
	var validCode = true;
	$('#pswCode').on('tap', function() {

		if(!$('#phone').val()) {
			mui.toast('请输入手机号');
			return false;
		};
		var tel = $('#phone').val();
		var reg = /(1[3-9]\d{9}$)/;
		if(!reg.test(tel)) {
			mui.toast('请输入正确的手机格式');
			return false;
		}

		var newNum = 60;
		if(validCode == true) {
			validCode = false;
			var timer = setInterval(function() {
				newNum--;
				//console.log(newNum);
				$('.pswCode').html(newNum);
				if(newNum == 0) {
					clearInterval(timer);
					$('.pswCode').html('获取验证码');
					validCode = true;

				}
			}, 1000);
			newuserCode({
				phoneNo: tel
			}, function(data) {
				//console.log(data.data, 888);
			});
		}

	});

})

//用户注册
var newuserLogin = function(params, callback) {
	$.ajax({
		type: "post",
		url: window.address + "/user/register",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: 'json',
		success: function(data) {
			//console.log(data)
			callback && callback(data);
		}
	});
}

//验证码
var newuserCode = function(params, callback) {
	$.ajax({
		type: "get",
		url: window.address + "/user/getVerificationCode",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: 'json',
		success: function(data) {
			//console.log(data)
			callback && callback(data);
		}
	});
}

//获取codeUrl
var getCodeUrl = function(callback) {
	$.ajax({
		type: "get",
		url: window.address + "/weChatPay/getCodeUrl",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: '',
		dataType: 'json',
		success: function(data) {
			//console.log(data);
			callback && callback(data)
		}
	});
}